﻿using System;
using System.Security.AccessControl;
using System.Windows.Forms;
using Microsoft.Win32;

namespace VSTranslate.Settings
{
    internal sealed class SettingsManager
    {
        public static readonly SettingsManager Instance = new SettingsManager();

        private const string Path = @"Software\ITProf\VSTranslate\";

        private SettingsManager()
        {
        }

        public bool IntegrateWithTools
        {
            get
            {
                //TODO: Refactoring!
                const bool defaultValue = true;
                try
                {
                    var key = Registry.CurrentUser.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);
                    if (key == null)
                    {
                        key = Registry.CurrentUser.CreateSubKey(Path, RegistryKeyPermissionCheck.ReadSubTree);
                    }
                    return Convert.ToBoolean(key.GetValue("IntegrateWithTools", defaultValue));
                } catch(Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error");
                    return defaultValue;
                }
            }
            set
            {
                //TODO: Refactoring!
                try
                {
                    var key = Registry.CurrentUser.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey);
                    if (key == null)
                    {
                        key = Registry.CurrentUser.CreateSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    }
                    key.SetValue("IntegrateWithTools", value);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error");
                }
            }
        }

        public bool IntegrateWithCodeWindowContextMenu
        {
            get
            {
                //TODO: Refactoring!
                const bool defaultValue = true;
                try
                {
                    var key = Registry.CurrentUser.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);
                    if (key == null)
                    {
                        key = Registry.CurrentUser.CreateSubKey(Path, RegistryKeyPermissionCheck.ReadSubTree);
                    }
                    return Convert.ToBoolean(key.GetValue("IntegrateWithCodeWindowContextMenu", defaultValue));
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error");
                    return defaultValue;
                }
            }
            set
            {
                //TODO: Refactoring!
                try
                {
                    var key = Registry.CurrentUser.OpenSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey);
                    if (key == null)
                    {
                        key = Registry.CurrentUser.CreateSubKey(Path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    }
                    key.SetValue("IntegrateWithCodeWindowContextMenu", value);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error");
                }
            }
        }
    }
}